#!/bin/bash
root_dir="/usr/local/edata"
if [ ! -d "$root_dir" ];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") WARNING $root_dir undetected"
    mkdir -p $root_dir
    if [ $? -eq 0 ];then
        echo "$(date +"%Y-%m-%d %H:%M:%S") INFO creating $root_dir success"
    else
        echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR creating $root_dir fail"
        exit 1
    fi
else
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO $root_dir detected"
fi

jdk_ver=$(java -version 2>&1)
if [[ $jdk_ver == *"1.8.0_221"* ]];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO jdk 1.8.0_221 detected"
else
    if [[ $jdk_ver == *"not found"* ]];then
        echo "$(date +"%Y-%m-%d %H:%M:%S") WARNING jdk is not found,installing..."
    else
        echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR other version of jdk detected (1.8.0_221 is required),uninstall it first"
        exit 1
    fi
fi

echo "$(date +"%Y-%m-%d %H:%M:%S") INFO checking scala..."
version=$(scala -version 2>&1)
if [[ $version == *"2.12.17"* ]];then
	echo "$(date +"%Y-%m-%d %H:%M:%S") INFO scala 2.12.17 detected"
else
	if [[ $version == *"not found"* ]];then
		echo "$(date +"%Y-%m-%d %H:%M:%S") WARNING scala is not found,please install scala using eata scala package..."
	else
		echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR other version of scala is detected,please uninstall it first"
		exit 1
	fi
fi

echo "$(date +"%Y-%m-%d %H:%M:%S") INFO checking flink directory ..."
if [ ! -d "$root_dir/flink" ];then
	echo "$(date +"%Y-%m-%d %H:%M:%S") INFO $root_dir/flink is not found...,unpressing flink package of edata"
	tar -zxvf flink-1.17.2-bin-scala_2.12.tgz
	mv flink-1.17.2 $root_dir/flink
    rm $root_dir/flink/conf/flink-conf.yaml
    rm $root_dir/flink/conf/masters
    rm $root_dir/flink/conf/workers
else
    echo "$(date +"%Y-%m-%d %H:%M:%S") $root_dir/flink detected"
fi

echo "$(date +"%Y-%m-%d %H:%M:%S") INFO checking FLINK_HOME ..."
if [[ -z "$FLINK_HOME" ]];then
	echo "export FLINK_HOME=$root_dir/flink" >> ~/.bashrc
	echo 'export PATH=$FLINK_HOME/bin:$PATH' >> ~/.bashrc
	echo "export FLINK_HOME=$root_dir/flink" >> /etc/profile
	echo 'export PATH=$FLINK_HOME/bin:$PATH' >> /etc/profile
	source ~/.bashrc
	source /etc/profile
else
	echo "$(date +"%Y-%m-%d %H:%M:%S") INFO FLINK_HOME is set"
fi

mode=$1
if [[ -z "$mode" ]];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR flink mode is empty"
    exit 1
fi
echo $mode >> mode
env_file="flink_sa.env"
if [ $mode == "sa" ];then
	env_file="flink_sa.env"
fi
if [ $mode == "ha" ];then
	env_file="flink_ha.env"
fi
conf_dir=$root_dir/flink/conf
while IFS= read -r line
do
	if [[ -n "$line" ]];then
        IFS="_" read -r prefix suffix <<< "$line"
		IFS="=" read -r name value <<< "$suffix"
        echo "$(date +"%Y-%m-%d %H:%M:%S") setting $name=$value"
        if [ $prefix == "FC" ];then
            echo "$name: $value" >> $conf_dir/flink-conf.yaml
        fi
	fi
done < "$env_file"
flink -v
if [[ $? -eq 0 ]];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO flink setup success"
else
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR flink setup error"
fi